﻿''' <summary>
''' 数字转换类
''' </summary>
''' <remarks></remarks>
Public Class NumericConvert

    'Private Declare Auto Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByRef destination As Object, ByRef source As Object, ByVal length As Integer)

    ''' <summary>
    ''' 字节数组转单精度浮点数
    ''' </summary>
    ''' <param name="b"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function Byte2Single(ByVal b() As Byte) As Single
        If b Is Nothing OrElse b.Length < 4 Then
            Throw New ArgumentException("无效的参数b()")
        End If

        Dim result As Single = 0
        'CopyMemory(result, b(0), 4)
        Dim buf(3) As Byte
        buf(0) = b(1)
        buf(1) = b(0)
        buf(2) = b(3)
        buf(3) = b(2)
        result = BitConverter.ToSingle(buf, 0)

        'result = BitConverter.ToSingle(b, 0)
        Return result
    End Function

    ''' <summary>
    ''' 字节数组转Short(有符号)
    ''' </summary>
    ''' <param name="b"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function Byte2Short(ByVal b() As Byte) As Short
        If b Is Nothing OrElse b.Length < 2 Then
            Throw New ArgumentException("无效的参数b()")
        End If

        Dim result As Short = 0
        'CopyMemory(result, b(0), 4)
        Dim buf(1) As Byte
        buf(0) = b(1)
        buf(1) = b(0)
       
        result = BitConverter.ToInt16(buf, 0)

        'result = BitConverter.ToSingle(b, 0)
        Return result
    End Function


    Public Shared Function Byte2Short(ByVal bHigh As Byte, ByVal bLow As Byte) As Short
        Dim b() As Byte = {bHigh, bLow}
        Return Byte2Short(b)
    End Function


    'Public Shared Function Byte2SingleDirectOrder(ByVal b() As Byte) As Single
    '    If b Is Nothing OrElse b.Length < 4 Then
    '        Throw New ArgumentException("无效的参数b()")
    '    End If

    '    Dim result As Single = 0

    '    result = BitConverter.ToSingle(b, 0)

    '    'result = BitConverter.ToSingle(b, 0)
    '    Return result
    'End Function


    Public Shared Function Byte2Single(ByVal b() As Byte, ByVal i As Integer) As Single
        If b.Length < (i + 1) * 4 Then
            Throw New ArgumentException("无效的参数b()")
        End If

        Dim b1(3) As Byte
        For t As Integer = 0 To b1.GetUpperBound(0)
            b1(t) = b(4 * i + t)
        Next

        Return Byte2Single(b1)
    End Function


    '''' <summary>
    '''' 字节数组转单精度浮点数（低位字节在前，高位字节在后）
    '''' </summary>
    '''' <param name="b"></param>
    '''' <returns></returns>
    '''' <remarks></remarks>
    'Public Shared Function Byte2Single2(ByVal b() As Byte) As Single
    '    If b Is Nothing OrElse b.Length < 4 Then
    '        Throw New ArgumentException("无效的参数b()")
    '    End If

    '    Dim result As Single = 0
    '    'CopyMemory(result, b(0), 4)
    '    Dim buf(3) As Byte
    '    buf(0) = b(0)
    '    buf(1) = b(1)
    '    buf(2) = b(2)
    '    buf(3) = b(3)
    '    result = BitConverter.ToSingle(buf, 0)

    '    Dim str As String
    '    str = String.Format("{0:f2}", result)

    '    Return Single.Parse(str)

    '    'Return result
    'End Function


    ''' <summary>
    ''' 分解浮点数
    ''' </summary>
    ''' <param name="value"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function Single2Byte(ByVal value As Single) As Byte()
        Dim result(3) As Byte

        Dim b(3) As Byte

        b = BitConverter.GetBytes(value)

        Return b

        For i As Integer = 0 To 3
            result(3 - i) = b(i)
        Next
        Return result
    End Function

    ''' <summary>
    ''' 位转字节
    ''' </summary>
    ''' <param name="cs"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function Boolean2Char(ByVal cs() As Boolean) As Byte
        Dim result As Byte = 0
        For i As Integer = 0 To 7
            If cs(i) = True Then
                result += 2 ^ i
            End If
        Next
        Return result
    End Function

    ''' <summary>
    ''' 字节转位
    ''' </summary>
    ''' <param name="c"></param>
    ''' <param name="pos"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function Char2Boolean(ByVal c As Byte, ByVal pos As Integer) As Boolean
        If c And 2 ^ pos Then
            Return True
        Else
            Return False
        End If
    End Function

    ''' <summary>
    ''' 字节转位数组
    ''' </summary>
    ''' <param name="c"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function Char2Booleans(ByVal c As Byte) As Boolean()
        Dim b(7) As Boolean

        For i As Integer = 0 To b.GetUpperBound(0)
            'b(7 - i) = c And 2 ^ i
            b(i) = c And 2 ^ i
        Next

        Return b
    End Function

    ''' <summary>
    ''' Bytes转为DateTime
    ''' </summary>
    ''' <param name="bytes"></param>
    ''' <param name="offset"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function Bytes2DateTime(ByVal bytes() As Byte, ByVal offset As Integer) As DateTime
        Dim dt As DateTime

        Try

            dt = New DateTime(bytes(offset) + 1900, bytes(offset + 1), bytes(offset + 2), bytes(offset + 3), bytes(offset + 4), bytes(offset + 5))

        Catch ex As Exception
            dt = DateTime.MinValue
        End Try

        Return dt
    End Function
    Public Shared Function Bytes2DateTime(ByVal bytes() As Short, ByVal offset As Integer) As DateTime
        Dim dt As DateTime

        Try

            dt = New DateTime(bytes(offset) + 1900, bytes(offset + 1), bytes(offset + 2), bytes(offset + 3), bytes(offset + 4), bytes(offset + 5))

        Catch ex As Exception
            dt = DateTime.MinValue
        End Try

        Return dt
    End Function

End Class
